// State dialog module
#include "burner.h"

int drvSaveAll = 0;

// The automatic save
int statedAuto(int save)
{
	static wchar_t szName[MAX_PATH];
	int ret;

	swprintf(szName, L"config\\games\\%hs.fs", BurnDrvGetTextA(DRV_NAME));

	if (save == 0) {
		ret = BurnStateLoad(szName, drvSaveAll, NULL);		// Load ram
		if (ret && drvSaveAll)	{
			ret = BurnStateLoad(szName, 0, NULL);				// Couldn't get all - okay just try the nvram
		}
	} else {
		ret = BurnStateSave(szName, drvSaveAll);				// Save ram
	}

	return ret;
}

static void createStateName(int slot)
{
	// create dir if dir doesn't exist
	if (!directoryExists(getMiscPath(PATH_SAVESTATE))) {
		CreateDirectory(getMiscPath(PATH_SAVESTATE), NULL);
	}

	swprintf(commonChoice, L"%s%hs slot %02x.fs", getMiscPath(PATH_SAVESTATE), BurnDrvGetTextA(DRV_NAME), slot);
}

int statedLoad(int slot)
{
	wchar_t szFilter[1024];
	int ret;

	if (driverOkay == 0) {
		return 1;
	}

	// if rewinding during playback, and readonly is not set,
	// then transition from decoding to encoding
	if (!replayReadOnly && replayStatus == 2)
	{
		replayStatus = 1;
	}
	if (replayReadOnly && replayStatus == 1)
	{
		stopReplay();
		replayStatus = 2;
	}

	if (slot) {
		createStateName(slot);
	} else {
		if (driverOkay) {
			swprintf(commonChoice, L"%hs*.fs", BurnDrvGetTextA(DRV_NAME));
		} else {
			swprintf(commonChoice, L"savestate");
		}
		wchar_t* pszFilter = szFilter;
		swprintf(pszFilter, localiseStringEx(IDS_DISK_FILE_STATE), APP_TITLE);
		memcpy(pszFilter + wcslen(pszFilter), L" (*.fs, *.fr)\0*.fs;*.fr\0\0", 25 * sizeof(wchar_t));

		if (commonFileDialog(hScrnWnd, false, localiseStringEx(IDS_STATE_LOAD), szFilter, getMiscPath(PATH_SAVESTATE), L"fs")) {		// Error
			vidSNewShortMsg(localiseStringEx(IDS_STATE_LOAD_ERROR), 0xFF3F3F);
			return 1;
		}
	}

	ret = BurnStateLoad(commonChoice, 1, &drvInitCallback);

	vidSNewShortMsg(localiseStringEx(IDS_STATE_LOADED));

	if (slot) {
		return ret;
	}

	// Describe any possible errors:
	if (ret == 3) {
		FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_DISK_THIS_STATE));
		FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_DISK_UNAVAIL));
	} else {
		if (ret == 4) {
			FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_DISK_THIS_STATE));
			FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_DISK_TOOOLD), APP_TITLE);
		} else {
			if (ret == 5) {
				FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_DISK_THIS_STATE));
				FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_DISK_TOONEW), APP_TITLE);
			} else {
				if (ret && !slot) {
					FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_DISK_LOAD));
					FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_DISK_STATE));
				}
			}
		}
	}

	if (ret) {
		FBAPopupDisplay(PUF_TYPE_ERROR);
	}

	return ret;
}

int statedSave(int slot)
{
	wchar_t szFilter[1024];
	int ret;

	if (driverOkay == 0) {
		return 1;
	}

	if (slot) {
		createStateName(slot);
	} else {
		swprintf(commonChoice, L"%hs", BurnDrvGetTextA(DRV_NAME));
		wchar_t* pszFilter = szFilter;
		swprintf(pszFilter, localiseStringEx(IDS_DISK_FILE_STATE), APP_TITLE);
		memcpy(pszFilter + wcslen(pszFilter), L" (*.fs, *.fr)\0*.fs;*.fr\0\0", 25 * sizeof(wchar_t));

		if (commonFileDialog(hScrnWnd, true, localiseStringEx(IDS_STATE_SAVE), szFilter, getMiscPath(PATH_SAVESTATE), L"fs")) {		// Error
			vidSNewShortMsg(localiseStringEx(IDS_STATE_SAVE_ERROR), 0xFF3F3F);
			return 1;
		}
	}

	ret = BurnStateSave(commonChoice, 1);

	if (ret && !slot) {
		FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_DISK_CREATE));
		FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_DISK_STATE));
		FBAPopupDisplay(PUF_TYPE_ERROR);
	}

	vidSNewShortMsg(localiseStringEx(IDS_STATE_SAVED));

	return ret;
}
